package com.gulimall.seckill.controller;

import com.guigui.common.utils.R;
import com.gulimall.seckill.service.SecKillService;
import com.gulimall.seckill.to.RedisCacheSkuInfoTo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
public class SeckillController {

    @Autowired
    private SecKillService secKillService;

//    @Autowired
//    private StringRedisTemplate redisTemplate;

    @GetMapping("/killProduct")
    public R getKillPro() {
        List<RedisCacheSkuInfoTo> secKillProduct = secKillService.getSecKillProduct();
        return R.ok().put("data", secKillProduct);
    }

    @GetMapping("/killquery/{skuId}")
    public RedisCacheSkuInfoTo getKillDetailBySkuId(@PathVariable("skuId") Long skuId) {
        return secKillService.getKillDetailBySkuId(skuId);
    }

    @GetMapping("/kill")
    public R kill(@RequestParam("skuId") Long skuId,
                  @RequestParam("token") String token,
                  @RequestParam("sessionId") Long sessionId,
                  @RequestParam("promotionId") Long promotionId) {
        String orderSn = null;
        try {
            orderSn = secKillService.kill(skuId, token, sessionId, promotionId);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        return R.ok().put("data",orderSn);
    }

    public String getKILL_SEMAPHORE_PREFIX(String promotionId) {
        return "seckill:promotion:" + promotionId + ":sessions:stock:";
    }

}
